// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>
#include <vector>

#include <nebula/io/type_fwd.h>

#include <nebula/types/type_fwd.h>

namespace nebula {
    namespace io {
        struct ReadRange;
    }

    namespace ipc::internal {

        /// \class IoRecordedRandomAccessFile
        /// \brief An RandomAccessFile that doesn't perform real IO, but only save all the IO
        /// operations it receives, including read operation's <offset, length>, for replaying
        /// later
        class TURBO_EXPORT IoRecordedRandomAccessFile : public io::RandomAccessFile {
        public:
            explicit IoRecordedRandomAccessFile(const int64_t file_size)
                    : file_size_(file_size), position_(0) {}

            turbo::Status close() override;

            turbo::Status abort() override;

            /// \brief Return the position in this stream
            turbo::Result<int64_t> tell() const override;

            /// \brief Return whether the stream is closed
            bool closed() const override;

            turbo::Status Seek(int64_t position) override;

            turbo::Result<int64_t> get_size() override;

            turbo::Result<int64_t> read_at(int64_t position, int64_t nbytes, void *out) override;

            turbo::Result<std::shared_ptr<Buffer>> read_at(int64_t position, int64_t nbytes) override;

            turbo::Result<int64_t> read(int64_t nbytes, void *out) override;

            turbo::Result<std::shared_ptr<Buffer>> read(int64_t nbytes) override;

            const io::IOContext &io_context() const override;

            /// \brief Return a vector containing all the read operations this file receives, each
            /// read operation is represented as an nebula::io::ReadRange
            ///
            /// \return a vector
            const std::vector<io::ReadRange> &GetReadRanges() const;

        private:
            const int64_t file_size_;
            std::vector<io::ReadRange> read_ranges_;
            int64_t position_;
            bool closed_ = false;
            io::IOContext io_context_;
        };

    }  // namespace ipc::internal
}  // namespace nebula
